package com.dotools.toutiaolibrary;

import android.content.Context;

import com.bykv.vk.openvk.TTVfConstant;
import com.bykv.vk.openvk.TTVfManager;
import com.bykv.vk.openvk.TTVfNative;
import com.bykv.vk.openvk.TTVfObject;
import com.bykv.vk.openvk.VfSlot;
import com.dotools.toutiaolibrary.bean.TT_FeedBean;

import java.util.ArrayList;
import java.util.List;

import api.bean.API_TT_FeedBean;
import api.ttfeed.Feed_API_TT;

public class TT_Feed extends Feed_API_TT {

    private TTVfNative mTTAdNative;
    private ArrayList<API_TT_FeedBean> tt_feedBeans = new ArrayList<>();

    @Override
    public void LoadTTFeed(Context context, String appid, String adposid, int count, int downloadMode, boolean isDebug, final TTFeedListener listener) {
        if (appid != null) {
            if (appid.trim().equals("")) {
                return;
            }
        } else {
            return;
        }


        TTVfManager manager = TTManagerHolder.get();
        if (manager != null) {
            mTTAdNative = manager.createVfNative(context.getApplicationContext());
            VfSlot adSlot = new VfSlot.Builder()
                    .setCodeId(adposid)
                    .setSupportDeepLink(true)
                    .setImageAcceptedSize(640, 320)
                    .setAdCount(count)
                    .build();
            if (downloadMode == 1) {
                adSlot.setDownloadType(TTVfConstant.DOWNLOAD_TYPE_POPUP);
            }
            //step5:请求广告，调用feed广告异步请求接口，加载到广告后，拿到广告素材自定义渲染
            mTTAdNative.loadVfList(adSlot, new TTVfNative.VfListListener() {
                @Override
                public void onError(int code, String message) {
                    listener.onError(code, message);
                }

                @Override
                public void onVfListLoad(List<TTVfObject> list) {
                    if (list == null || list.isEmpty()) {
                        listener.onError(0, "没有广告");
                        return;
                    }
                    tt_feedBeans.clear();
                    for (int i = 0; i < list.size(); i++) {
                        TT_FeedBean tt_feedBean = new TT_FeedBean(list.get(i));
                        tt_feedBeans.add(tt_feedBean);
                    }
                    listener.onFeedLoad(tt_feedBeans);
                }
            });
        }else{
            listener.onError(666, "TTVfManager is null  可能原因sdk未初始化成功");
        }
    }
}
